package com.base.component.ai.platform.prompt;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.base.component.ai.agent.core.prompt.PromptLogHandler;
import com.base.component.ai.agent.utils.PromptChatUtils;
import com.base.component.ai.platform.ChatCompletionsHandler;
import com.base.component.ai.platform.core.bo.common.ChatCompletionsBO;
import com.base.component.ai.platform.core.converter.PromptResultConverterEditor;
import com.base.core.head.dto.ai.MessageBaseDTO;
import com.gitee.magic.core.exception.ApplicationException;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;

/**
 * 组件服务
 * 
 * @author zywei
 *
 */
@Slf4j
@Service
public class PromptTemplateService<M extends MessageBaseDTO> {

	@Autowired(required = false)
	private List<PromptLogHandler<ChatCompletionsBO<M>,M>> promptLogHandlers;
	@Autowired
	private List<ChatCompletionsHandler<M>> chatCompletionsHandlers;
	
	/**
	 * 获取所有支持的模型
	 * @return
	 */
	public List<String> getSupportModel(){
		List<String> models=new ArrayList<>();
		for(ChatCompletionsHandler<M> handler:chatCompletionsHandlers) {
			models.addAll(handler.getSupportModel());
		}
		return models;
	}
	
	/**
	 * 文本
	 * @param param
	 * @param cls
	 * @return
	 */
	public String execute(ChatCompletionsBO<M> param,Class<M> cls) {
		for(ChatCompletionsHandler<M> handler:chatCompletionsHandlers) {
			if(handler.support(param.getModel())) {
				String content=handler.execute(param);
				log.info("agent :{} model:{} requestId:{} response: {}",param.getCode(),param.getModel(),param.getRequestId(),content);
				content=PromptChatUtils.removeEmojii(PromptResultConverterEditor.getData(content));
				if(!CollectionUtils.isEmpty(promptLogHandlers)) {
					for(PromptLogHandler<ChatCompletionsBO<M>,M> logHandler:promptLogHandlers) {
						if(logHandler.support(cls)) {
							logHandler.saveLog(param, content);
						}
					}
				}
				return content;
			}
		}
		throw new ApplicationException("找不到执行模型："+param.getModel() +" code:"+param.getCode());
	}
	
	/**
	 * 流式
	 * @param param
	 * @param cls
	 * @param process 流式输出内容
	 * @param successed 整体内容
	 */
	public void execute(ChatCompletionsBO<M> param,Class<M> cls,Consumer<String> process,Consumer<String> successed) {
		for(ChatCompletionsHandler<M> handler:chatCompletionsHandlers) {
			if(handler.support(param.getModel())) {
				StringBuilder sb=new StringBuilder();
				handler.execute(param,(c)->{
					sb.append(c);
					process.accept(c);
				});
				log.info("agent :{} model:{} requestId:{} response stream: {}",param.getModel(),param.getCode(),param.getRequestId(),sb.toString());
				if(!CollectionUtils.isEmpty(promptLogHandlers)) {
					for(PromptLogHandler<ChatCompletionsBO<M>,M> logHandler:promptLogHandlers) {
						if(logHandler.support(cls)) {
							logHandler.saveLog(param, sb.toString());
						}
					}
				}
				return;
			}
		}
		throw new ApplicationException("找不到执行模型："+param.getModel() +" code:"+param.getCode());
	}
	
	/**
	 * 流式
	 * @param param
	 * @return
	 */
	public Flux<String> stream(ChatCompletionsBO<M> param) {
		for(ChatCompletionsHandler<M> handler:chatCompletionsHandlers) {
			if(handler.support(param.getModel())) {
				return handler.stream(param);
			}
		}
		throw new ApplicationException("找不到执行模型："+param.getModel() +" code:"+param.getCode());
	}

}
